﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FacebookApp;
using System.Data;
using System.Configuration;

public partial class Modules_ListProduct : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {            
            dlListProduct.DataSource = EbookService.Ebook_GetByTop("4", " [Status]='True' ", " CreateDate DESC ");
            dlListProduct.DataBind();

            dlMostBought.DataSource = EbookService.Ebook_GetByTop("4", " [Status]='True' ", " NumBought DESC ");
            dlMostBought.DataBind();

            dlsaleoff.DataSource = EbookService.Ebook_GetByTop("4", " [Status]='True' and PriceNew <> 0 ", " PriceNew ");
            dlsaleoff.DataBind();

            dlMostView.DataSource = EbookService.Ebook_GetByTop("4", " [Status]='True' ", " NumView DESC ");
            dlMostView.DataBind();
        }
        
    }

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        string strCA = e.CommandArgument.ToString();
        if (e.CommandName == "Choose")
        {
            AddCart(e.CommandArgument.ToString(), "1");
            //if (Session["_countPro"] == null)
            //    Session["_countPro"] = 1;
            //else
            //Session["_countPro"] = int.Parse(Session["_countPro"].ToString()) + 1;
            //WebMsgBox.Show("choose");
            
        }
        else
        {
            Ebook dt = EbookService.Ebook_GetById(strCA);
            string Category_Id = dt.Category_Id.ToString();
            string URL = "Pages/ProductDetail.aspx?Catogory_Id =" + Category_Id + "&Id=" + strCA;
            Response.Redirect(URL);
        }
    }

    protected string Format_Price(string s)
    {
        return FunctionClass.Format_Price(s);
    }

    protected string CatChu(string s)
    {
        return FunctionClass.CatChu(s);
    }

    protected void AddCart(string productid, string quantity)
    {
        if (Session["Cart"] == null)
            Session["Cart"] = FunctionClass.AddCart(productid, quantity, null); 
        else
            Session["Cart"] = FunctionClass.AddCart(productid, quantity, (DataTable)Session["Cart"]);

        if (Session["CountAddProduct"] == null)
            Session["CountAddProduct"] = 1;
        else
            Session["CountAddProduct"] = int.Parse(Session["CountAddProduct"].ToString()) + 1;

    }

    //protected string createlink(string id, string category_id)
    //{

    //    return ConfigurationSettings.AppSettings.GetValues("fb_headerpath")[0].ToString() + "Pages/ProductDetail.aspx?Catogory_Id=" + category_id + "&Id="+ id;
    //}
}